﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Net.Http;
using System.IO;
using System.Web.Http;
using ExpenseSystem.Core;
using ExpenseSystem.Core.Authority;
using ExpenseSystem.Core.InformationModules;
using ExpenseSystem.Fether;
using ExpenseSystem.Common;
using ExpenseSystem.Lor;
using ExpenseSystem.Model;
using ExpenseSystem.Common.BaseModel;

namespace ExpenseSystem.Controllers
{
    [RoutePrefix("api/Information")]
    public class InformationController : ApiController
    {
        GetInhibition inhibition = new GetInhibition();
        public InformationController()
        {
        }
        [Route("Version")]
        [HttpGet]
        public ResponseModel GetVersion()
        {
            ResponseModel responseModel = new ResponseModel();
            Authority authority = new Authority();
            if(!authority.GetAuthority("SYS_INFORMATION_VERSION"))
            {
                responseModel.Result = Definition.RESP_NOT_AUTH;
                responseModel.Count = 0;
                responseModel.Data = null;
                responseModel.Message = "没有授予对应的权限";
                return responseModel;
            }
            InformationFetcher informationFetcher = new InformationFetcher();
            
            string message;
            InformationModel model = informationFetcher.GetDefault( out message);
            if(model==null)
            {
                responseModel.Result = Definition.RESP_DATA_NOT_FOUND;
                responseModel.Count = 0;
                responseModel.Data = null;
                responseModel.Message = String.Format( "没有找到版本信息:{0}",message);
                return responseModel;
            }
            else
            {
                responseModel.Result = Definition.RESP_SUCCESS;
                responseModel.Count = 1;
                responseModel.Data = JsonHelper.ToJson(model,inhibition);
                responseModel.Message = "操作成功";
                return responseModel;
            }
        }
        [Route("Update")]
        [HttpPost]
        public ResponseModel SetCompany(InformationModel data)
        {
            ResponseModel response = new ResponseModel();
            Authority authority = new Authority();
            if (!authority.GetAuthority("SYS_INFORMATION_SET"))
            {
                response.Result = Definition.RESP_NOT_AUTH;
                response.Count = 0;
                response.Data = null;
                response.Message = "没有授予对应的权限";
                return response;
            }
            InformationFetcher informationFetcher = new InformationFetcher();
            MergeHelper mergeHelper = new MergeHelper(inhibition);
            
            InformationModel model = mergeHelper.SystemObjMerge(data, informationFetcher);
            InformationManager infoManager = InformationManager.CreateInstance();
            string message = "";
            bool result = infoManager.Save(model,out message);
            if (!result) SqlProvider.CloseTransaction(HttpContext.Current.Items[Definition.SESSION_TRAN_STR].ToString(), false);
            if (result)
            {
                response.Result = Definition.RESP_SUCCESS;
                response.Count = 1;
                response.Data = JsonHelper.ToJson(model,inhibition);
            }
            else
            {
                response.Result = Definition.RESP_SET_DATA_ERROR;
                response.Count = 0;
                response.Data = response.Message = message;
            }
            return response;
        }
    }
}
